#include "libtimers.h"
#include <stdio.h>
#include <unistd.h>



int main()
{
    /* get a new timer pool object */
    LT_TIMERPOOL *pool = lt_new(NULL);
    for(int i = 1; i <= 10; i++) {
        /* get a new timer descriptor  */
        uint32_t tid = lt_timer_open(pool);

        /* assign a random data to the timer */
        int data = (10 - i) * 100;

        /* set the timer, it will time up at n-th seconds later */
        lt_set_timer(pool, tid, (void*)data, i * 1000);
    }


    while(lt_timer_size(pool) > 0) {
        /* sleep a while, or do something like select() between timer polls */
        usleep(10000);

        LT_TIMER_EVENT *events;
        /* poll timer events */
        int event_count = lt_poll(pool, &events);

        if (event_count > 0)
            printf("timer poll get %d events\n", event_count);

        for(int i = 0; i < event_count; ++i) {
            /* show something at time up */
            printf("Times up, data: %d\n", (int)events[i].data);

            /* force close(free) next timer before it times up */
            lt_timer_close(pool, events[i].timer_id + 1);
        }

        /* free events array after a poll */
        lt_free_events(pool, events);
    }

    /* free the pool */
    lt_destroy(pool);

    return 0;
}

